package com.cy.milktea.controller.system;

import com.cy.milktea.pojo.System.JsonResult;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /* 异常处理 */
    @ExceptionHandler(Exception.class)
    public JsonResult failureHander(Exception e) {
        String message;
        if (e instanceof LockedAccountException) {
            message = "账号被禁用!";
        } else if (e instanceof UnknownAccountException) {
            message = "账号或密码错误!";
        } else if (e instanceof IncorrectCredentialsException) {
            message = "账号或密码错误!";
        } else if (e instanceof UnauthorizedException) {
            message = "权限不足!";
        } else {
            /* 存储日志 */
            log.error(e.getMessage());
            message = "未知错误!";
        }
        return JsonResult.error(message);
    }
}
